package alibaba;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class DirectoryInfo {

	private Set<ListingEntry> files;
	private Set<String> subdirectories;
	private static Map<String,Map<String, DirectoryInfo>> memoryStoredDirMaps = 
		new HashMap<String,Map<String, DirectoryInfo>>();

	public static Map<String,Map<String, DirectoryInfo>> getMemoryStoredDirMaps() {
		return memoryStoredDirMaps;
	}
	
	public Map<String, DirectoryInfo> getUserDirectoryMap(String user){
		return memoryStoredDirMaps.get(user);
	}

	public DirectoryInfo(String virtualDirName, String user) {

		files = new HashSet<ListingEntry>();
		subdirectories = new TreeSet<String>();
		Map<String, DirectoryInfo> memoryStoredDirMap = getUserDirectoryMap(user);
		if (memoryStoredDirMap == null){
			memoryStoredDirMap = new HashMap<String, DirectoryInfo>();
			memoryStoredDirMaps.put(user, memoryStoredDirMap);
		}
		memoryStoredDirMap.put(virtualDirName, this);
		if (!virtualDirName.equals("/")) {
			String parentDirName = Utils.extractVirtualDirName(virtualDirName);
			DirectoryInfo parentDir = null;
			if (memoryStoredDirMap.get(parentDirName) == null)
				parentDir = new DirectoryInfo(parentDirName, user);
			else
				parentDir = memoryStoredDirMap.get(parentDirName);
			parentDir.addDir(virtualDirName);
		}
	}

	public Set<ListingEntry> getFiles() {
		return files;
	}

	public Set<String> getSubdirectories() {
		return subdirectories;
	}

	public void addFile(ListingEntry entry) {
		files.add(entry);
	}

	public void addDir(String virtualDirName) {
		subdirectories.add(virtualDirName);
	}

	public void removeFile(ListingEntry entry) {
		files.remove(entry);
		
	}
}
